#pragma once

#include <rocksdb/status.h>
#include <memory>
#include "SequenceStream.h"
#include <string>

namespace streamdb {

class File;

class FileStream : public SequenceStream<std::string>{
public:
    FileStream(const std::string & fname) ;
    ~FileStream() override;

    Status open();
    void close();

    Status append(const std::string & obj) override;
    uint64_t getSize() const override {
        return size;
    }
    const std::string &getName() const ;


    class iterator : public Iterator<std::string>{
    public:
        iterator(File & file) : file(file), buf(new char[BUF_SIZE]), size(0) {}
        ~iterator() override{
            if(buf){
                delete [] buf;
            }
        }

        iterator(const iterator & other)  = delete;
        iterator & operator=(const iterator & other) = delete;

        bool hasNext() const override;
        std::string & next() override;
    private:
        File & file;
        char *buf; //file buffer
        mutable uint64_t size; //record size
        std::string value; //record value

        static const int BUF_SIZE = 1024;
    };
    
    std::shared_ptr<SequenceStream<std::string>::IteratorType> advance(int64_t offset) override;
    std::shared_ptr<SequenceStream<std::string>::IteratorType> begin() override;

private:
    std::unique_ptr<File> file;
    uint64_t size;
    bool isOpen;
};


}//namespace streamdb
